package com.mymail.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import com.mymail.dao.UserDAO;

public class User {
	// 默认用户状态
	public static final char SATA_DEFAULT = 'A';

	// 表示性别的两个字段
	public static final char MALE = 'M'; // 男
	public static final char FEMALE = 'F'; // 女

	/**
	 * addnNew()在数据中增加新用户我返回值类型 NAME_EXIST 表示用户名已经存在 EMAIL_EXIST 表示注册邮件重复
	 * ADD_NEW_OK 插入成功
	 * 
	 */

	public static final int NAME_EXIST = -1;
	public static final int EMAIL_EXIST = -2;
	public static final int ADD_NEW_OK = 0;
	public static final int UNKNOW_ERROR = -3;

	// 邮件每一页显示的数目
	public static final int PER_PAGE = 20;

	private Integer id;
	private String name;
	private String password;
	private String email;
	private char status = SATA_DEFAULT;
	private String phone;
	private String qq;
	private char gender = User.MALE;
	private Date birth = new Date();
	private ArrayList<Group> groups;
	private ArrayList<Folder> folders;
	private ArrayList<MailBox> mailboxs;
	private ArrayList<Mail> mails;
	private int currentpage = -1;
	private String foldername;

	// private UserDAO userDAO=new UserDAO();

	public int getCurrentpage() {
		return currentpage;
	}
	public String getFolderName(){
		return foldername;
	}

	public ArrayList<Mail> getMails(String foldername, int page) {
		currentpage = page;
		this.foldername=foldername;
		mails = Mail.getDAO().findMailsByFolderId(
				findFolder(foldername).getId(), (page - 1) * PER_PAGE,
				page * PER_PAGE);
		return mails;
	}

	public Mail getMail(int index) {
		if (index < mails.size()) {
			return mails.get(index);
		}
		return null;
	}

	public int addToDatabase() {
		UserDAO dao = getDAO();
		if (dao.addNew(this)) {
			return getId().intValue();
		} else {
			return dao.getErrorCode();
		}
	}

	public boolean toDatabase() {
		return getDAO().toDatabase(this);
	}

	public static User getById(int id) {
		User user = new User();
		if (getDAO().getByID(new Integer(id), user)) {
			return user;
		}
		return null;
	}

	public static User getByName(String name) {
		User user = new User();
		if (getDAO().getByName(name, user)) {
			return user;
		}
		return null;
	}

	public static boolean getByName(String name, User user) {
		return getDAO().getByName(name, user);
	}

	public static UserDAO getDAO() {
		return new UserDAO();
	}

	public MailBox findMailbox(String name) {
		MailBox mailbox = null;
		Iterator<MailBox> it = getMailboxs().iterator();
		while (it.hasNext()) {
			mailbox = it.next();
			if (mailbox.getName().equals(name)) {
				return mailbox;
			}
		}
		return null;
	}

	public Group findGroup(String name) {
		Group group = null;
		Iterator<Group> it = getGroups().iterator();
		while (it.hasNext()) {
			group = it.next();
			if (group.getName().equals(name)) {
				return group;
			}
		}
		return null;
	}

	public Folder findFolder(String name) {
		Folder folder = null;
		Iterator<Folder> it = getFolders().iterator();
		while (it.hasNext()) {
			folder = it.next();
			if (folder.getName().equals(name)) {
				return folder;
			}
		}
		return null;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the status
	 */
	public char getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(char status) {
		this.status = status;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone
	 *            the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * @return the qq
	 */
	public String getQq() {
		return qq;
	}

	/**
	 * @param qq
	 *            the qq to set
	 */
	public void setQq(String qq) {
		this.qq = qq;
	}

	/**
	 * @return the gender
	 */
	public char getGender() {
		return gender;
	}

	/**
	 * @param gender
	 *            the gender to set
	 */
	public void setGender(char gender) {
		this.gender = gender;
	}

	/**
	 * @return the birth
	 */
	public Date getBirth() {
		return birth;
	}

	/**
	 * @param birth
	 *            the birth to set
	 */
	public void setBirth(Date birth) {
		this.birth = birth;
	}

	/**
	 * @return the groups
	 */
	public ArrayList<Group> getGroups() {
		if (groups == null) {
			groups = Group.getDAO().getGroupsByUser(this);
		}
		return groups;
	}

	/**
	 * @param groups
	 *            the groups to set
	 */
	public void setGroups(ArrayList<Group> groups) {
		this.groups = groups;
	}

	/**
	 * @return the folders
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<Folder> getFolders() {
		if (folders == null) {
			folders = Folder.getDAO().getFoldersByUser(this);
		}
		return folders;

	}

	/**
	 * @param folders
	 *            the folders to set
	 */
	public void setFolders(ArrayList<Folder> folders) {
		this.folders = folders;
	}

	/**
	 * @return the mailboxs
	 */
	public ArrayList<MailBox> getMailboxs() {
		if (mailboxs == null) {
			mailboxs = MailBox.getDAO().getMailboxsByUser(this);
		}
		return mailboxs;
	}

	/**
	 * @param mailboxs
	 *            the mailboxs to set
	 */
	public void setMailboxs(ArrayList<MailBox> mailboxs) {
		this.mailboxs = mailboxs;
	}

}
